package com.vma.jifu.backsys.controller;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.jifu.bizModel.model.*;
import com.vma.tools.ReturnsUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by kitabi on 2017/6/26.
 */
public class SystemController extends BaseController {


    public void qryAboutUs(){


            Protocol tgArticle = Protocol.dao.findFirst("select * from protocol order by id desc");
            setAttr("initData",tgArticle);


        renderVelocity("/newshop/system/about/about.html");
    }

    public void saveAboutUs(){

        String content= getPara("content");

        Protocol tgArticle =new Protocol();
        tgArticle.setContent(content);
        tgArticle.setCreateTime(new Date());
        tgArticle.save();

        renderJson(ReturnsUtil.returnSuccess("保存成功"));
    }

    public void qryBase(){


        CustPhone custPhone = CustPhone.dao.findFirst("select * from cust_phone order by id desc");
        setAttr("custPhone",custPhone);

        IndexImg indexImg = IndexImg.dao.findFirst("select * from index_img order by id desc");
        setAttr("IndexImg",indexImg);

        List<CustQq> custQqs =CustQq.dao.find("select * from  cust_qq   order by id desc limit 3");
        setAttr("custQq",custQqs);

        renderVelocity("/newshop/system/base/base.html");
    }


    public void qryJoinList(){
        String status = getPara("status");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");
        String province = getPara("province");
        String city = getPara("city");
        String area = getPara("area");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(status != null && "".equals(status)){
            condition += " and t.status = ? ";
            param.add(status);
        }

        if(province != null &&  !"".equals(province)){

            condition += " and t.province = ? ";
            param.add(province);
        }
        if(city != null &&  !"".equals(city)){

            condition += " and t.city = ? ";
            param.add(city);
        }
        if(area != null &&  !"".equals(area)){

            condition += " and t.area = ? ";
            param.add(area);
        }


        Page<Record> joinFee = Db.paginate(pageNo,pageSize,"select t.*,p.province,c.city,a.area ",
                " from join_fee t,provinces p,cities c,areas a " +
                        "where t.province= p.provinceid and t.city = c.cityid and t.area= a.areaid"+condition
                ,param.toArray());


        renderJson(ReturnForm(joinFee.getList(),joinFee.getTotalRow()));
    }

    public void updateStatus(){
        String id = getPara("id");
        int cast_num= getParaToInt("cast_num");

        JoinFee joinFee = JoinFee.dao.findById(id);
        joinFee.setCastNum(cast_num);
        joinFee.update();

        renderJson(ReturnsUtil.returnSuccess("保存成功"));
    }

    public void saveBase(){
        String img_url= getPara("img_url");
        String cust_qq= getPara("cust_qq");
        String cust_phone= getPara("cust_phone");

        if(img_url != null && !"".equals(img_url)){
            IndexImg indexImg = new IndexImg();
            indexImg.setUrl(img_url).setCreateTime(new Date()).save();
        }

        if(cust_qq != null ){
            String [] qqs= cust_qq.split(",");
            for(int i=qqs.length-1 ;i >-1;i--){
                CustQq custQq= new CustQq();
                custQq.setQq(qqs[i]).setCreateTime(new Date()).save();

            }
        }

        if(cust_phone !=null && !"".equals(cust_phone)){
            CustPhone custPhone = new CustPhone();
            custPhone.setPhone(cust_phone).setCreateTime(new Date()).save();

        }

        renderJson(ReturnsUtil.returnSuccess("保存成功"));
    }



}
